home *** CD-ROM | disk | FTP | other *** search
/ Loadstar 11 / 011.d81 / pps #24 < prev    next >
Text File  |  2022-08-26  |  6KB  |  316 lines

  1.  
  2.   PEEKs, POKEs, and SYSes -- Part 24
  3.  
  4.             "VARIABLES"
  5.  
  6.     by Alan Gardner & Jimmy Weiler
  7.  
  8. ======================================
  9. Location: 45-46    Hexadecimal: $2d-2e
  10. Official label: VARTAB        Type:RAM
  11. Useful BASIC commands:      PEEK, POKE
  12. --------------------------------------
  13. Location: 47-48    Hexadecimal: $2f-30
  14. Official label: ARYTAB        Type:RAM
  15. Useful BASIC commands:      PEEK, POKE
  16. ======================================
  17.  
  18.   What do you suppose REALLY happens,
  19.  
  20. deep down in the computer's memory
  21.  
  22. when you equate variables to values,
  23.  
  24. like this?
  25.  
  26. 100 XY=598.345:B%=17:C$="TRY AGAIN.."
  27.  
  28. Really?  OOFDA!  That sounds pretty
  29.  
  30. complicated.  We thought so, too.
  31.  
  32. Jimmy and I decided to check it out.
  33.  
  34.        Here's what we found.
  35.  
  36. The Commodore 64 stores data just like
  37.  
  38. most other microcomputers.  Text,
  39.  
  40. color, keyboard, KERNAL, and graphics
  41.  
  42. are usually cut and dried -- the
  43.  
  44. function of any location is always the
  45.  
  46. same.  For example, your text screen
  47.  
  48. usually starts at memory address 1024.
  49.  
  50.   Variables, on the other hand, can be
  51.  
  52. found in nearly every part of BASIC
  53.  
  54. RAM.  BASIC RAM is the free memory
  55.  
  56. from about 2049 (just above the text
  57.  
  58. screen) to 40960 (the BASIC ROM).
  59.  
  60.     (LOOK OUT!!! IT'S CHART TIME!)
  61.  
  62. Locations
  63. to peek.     What they point to.
  64. --------------------------------------
  65.  43,44       start of basic program
  66.  45,46       start of simple variables
  67.  47,48       start of array variables
  68.  49,50       end of array variables+1
  69.  51,52       end of strings
  70.  55,56       start of strings
  71. --------------------------------------
  72.  
  73. Because programs and variables can be
  74.  
  75. anywhere in 38000+ bytes of RAM, your
  76.  
  77. computer needs to keep track of where
  78.  
  79. they are at any time.  It does this
  80.  
  81. by means of pointers.  A pointer is
  82.  
  83. nothing more complicated than a memory
  84.  
  85. location that contains the address of
  86.  
  87. ANOTHER memory location.  (Gosh!  More
  88.  
  89. gobbledygook!)  Okay.... It's a lot
  90.  
  91. like the address on an envelope.  You
  92.  
  93. don't live on the envelope, you live
  94.  
  95. at the address referred to by the
  96.  
  97. envelope.  Likewise, a BASIC program
  98.  
  99. lives at the address referred to by
  100.  
  101. memory locations 43 and 44, and memory
  102.  
  103. locations 45 and 46 point to the
  104.  
  105. address where storage of BASIC
  106.  
  107. variables begins.
  108.  
  109.   Since one location can only hold a
  110.  
  111. value between 0 and 255, two locations
  112.  
  113. must be used to give an address
  114.  
  115. between 0 and 65535.  Location 45 is
  116.  
  117. the low byte and 46 is the high byte
  118.  
  119. of that address.  To find the actual
  120.  
  121. address of the start of BASIC
  122.  
  123. variables, do the following:
  124.  
  125. 200 AD=PEEK(45)+PEEK(46)*256
  126.  
  127.   Now that we know WHERE the variables
  128.  
  129. are stored, lets find out HOW they are
  130.  
  131. stored.
  132.  
  133.   All simple variables are stored, in
  134.  
  135. the order they are first used, from
  136.  
  137. VARTAB (the start of variables) to
  138.  
  139. ARYTAB (the start of arrays).  As you
  140.  
  141. use new variables, they push ARYTAB
  142.  
  143. up through memory.  Each simple
  144.  
  145. variable takes seven bytes of memory
  146.  
  147. between VARTAB and ARYTAB.  (Waddya
  148.  
  149. mean only seven bytes!?  What about a
  150.  
  151. string 93 characters long?  How do you
  152.  
  153. fit THAT into seven measley bytes?!)
  154.  
  155. <Well, um, we'll get to that later
  156.  
  157. (really!!).>
  158.  
  159.   After executing this line of code
  160.  
  161. 10 XY=598.345 :B%=17 :C$="TRY AGAIN.."
  162.  
  163. variable space would look something
  164.  
  165. like this:
  166.  
  167. ARYTAB --->
  168.             --------------
  169.             string pointer
  170.             128               third
  171. VARTAB+14-> 067 = "C"         variable
  172.             --------------
  173.               value of B
  174.             128               second
  175. VARTAB+7--> 194 = "B"         variable
  176.             --------------
  177.               value of X
  178.             089 = "Y"         first
  179. VARTAB ---> 088 = "X"         variable
  180.             --------------
  181. - - - - - - - - - - - - - - - - - - -
  182.  
  183.   Now, let's look at the three
  184.  
  185. different types of variables.  These
  186.  
  187. types are: INTEGER, STRING, and REAL.
  188.  
  189.   First of all, the INTEGERS.  The
  190.  
  191. first two bytes of an integer variable
  192.  
  193. are the name.  For an integer
  194.  
  195. variable, the high-bits of both bytes
  196.  
  197. of the name are set.  That means you
  198.  
  199. subtract 128 from each byte to find
  200.  
  201. the actual name of the variable.  If
  202.  
  203. the variable name has only one letter,
  204.  
  205. byte two equals 128.  Bytes three and
  206.  
  207. four are the high and low bytes of the
  208.  
  209. integer variable's value.  Since
  210.  
  211. integers values never go beyond -32767
  212.  
  213. or +32767, you only need two bytes to
  214.  
  215. represent their values.
  216.  
  217.   Integers are stored in the unusual
  218.  
  219. scheme of hi-byte first, lo-byte last.
  220.  
  221. Let's break those two bytes into bits
  222.  
  223. to see how some integers are stored.
  224.  
  225. We'll look at 260, and -128, and -1.
  226.  
  227. BINARY REPRESENTATION OF INTEGERS:
  228.   byte 3             byte 4
  229.   8 7 6 5 4 3 2 1 0  8 7 6 5 4 3 2 1 0
  230.   ------------------------------------
  231. A:0 0 0 0 0 0 0 0 1  0 0 0 0 0 0 1 0 0
  232. B:1 1 1 1 1 1 1 1 1  1 0 0 0 0 0 0 0 0
  233. C:1 1 1 1 1 1 1 1 1  1 1 1 1 1 1 1 1 1
  234.  
  235. There is NO escape from gobbledygook!
  236.  
  237.  
  238. Example A is the value 260.  This one
  239.  
  240. is pretty simple.  Byte 3 is the high
  241.  
  242. byte, so multiply its value by 256.
  243.  
  244. (256*1=256.)  Byte 4 is the low byte,
  245.  
  246. so add its value to the result from
  247.  
  248. byte 3. (256+4=260.)
  249.  
  250.  
  251. Example B is the value -128.  Negative
  252.  
  253. numbers are a bit more complicated
  254.  
  255. than positives, but not too hard once
  256.  
  257. you know how they work.  Bit 8 of byte
  258.  
  259. 3 is the "SIGN BIT".  If this bit is a
  260.  
  261. zero the integer is positive, if it is
  262.  
  263. one, the integer is negative.  If the
  264.  
  265. integer is negative, it is stored in
  266.  
  267. what is called "TWOS COMPLEMENT"
  268.  
  269. representation.  To decode a negative
  270.  
  271. number, change every 1 to a 0 and
  272.  
  273. every 0 to a 1, and add 1 and change
  274.  
  275. the sign.  (Everybody remember how to
  276.  
  277. add in binary?)
  278.  
  279. It goes something like this:
  280.  
  281. Reverse..   11111111 10000000
  282. Which       -----------------
  283. gives you.. 00000000 01111111
  284. Add 1..                    +1
  285.             -----------------
  286.             00000000 10000000 = -128
  287.  
  288.  
  289. Example C works just like example B.
  290.  
  291. Reverse..   11111111 11111111
  292. Which       -----------------
  293. gives you.. 00000000 00000000
  294. Add 1..                    +1
  295.             -----------------
  296.             00000000 00000001 = -1
  297.  
  298.   The last three bytes of an integer
  299.  
  300. variable are unused, but they still
  301.  
  302. need to be there.  EVERY simple
  303.  
  304. variable uses 7 bytes, like we said
  305.  
  306. before.  The seven bytes of variable
  307.  
  308. storage generated by the statement
  309.  
  310. B%=17 are:
  311.  
  312.    194, 128, 000, 017, 000, 000, 000
  313.    < name >  <value >  <  unused   >
  314.  
  315. --------<continued in part 25>--------
  316.